package com.tadpole.music.presenter.home;

import android.widget.Toast;

import com.tadpole.music.bean.SystemConfigBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.SystemConfigIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 系统配置
 * Created By 孙岩
 * on 2021/1/29
 */
public class SystemConfigPresenter extends BasePresenter<SystemConfigIView> {
    @SuppressWarnings("unchecked")
    public void getSystemConfig(){
        if(isViewAttached()){
            String url = UrlConfig.getSystemConfig;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    SystemConfigBean.DataBean bean = new SystemConfigBean.DataBean(
                                            JsonParseUtil.getStr(object,"suit_price")
                                    );

                                    getView().showSystemConfig(bean);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
